package com.googlecode.framework.web.tag;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.googlecode.framework.web.pagination.AbstractWebPagination;
import com.googlecode.framework.web.pagination.Href;
import com.googlecode.framework.web.pagination.PaginationConstants;

public class SortTag extends TagSupport {

    /**
     * serialVersionUID
     */
    private static final long serialVersionUID = -6117459239625356425L;
    
    /**
     * 排序字段
     */
    private String column;

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doStartTag()
     */
    public int doStartTag() throws JspException {
        AbstractWebPagination awp = (AbstractWebPagination) pageContext.getRequest().getAttribute(PaginationConstants.DEFAULT_PAGINATION_NAME);
        if (awp != null) {
            try {
                pageContext.getOut().write(markHref(awp.getHref()));
            } catch (IOException e) {
                throw new JspException(e);
            }
        }
        return EVAL_BODY_INCLUDE;
    }

    /*
     * (non-Javadoc)
     * @see javax.servlet.jsp.tagext.TagSupport#doEndTag()
     */
    public int doEndTag() throws JspException {
        try {
            pageContext.getOut().write("</a>");
        } catch (IOException e) {
            throw new JspException(e);
        }
        return EVAL_PAGE;
    }

    /*
     * ▲<a class="sortabe" href="xxxx.do?param1=value1&__sidx=&sord="></a>
     */
    protected String markHref(Href href) {
        HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
        
        Href hrefToUse = (Href) href.clone();
        hrefToUse.removeParameter(PaginationConstants.PARAM_SINDEX);
        hrefToUse.removeParameter(PaginationConstants.PARAM_SORDER);
        hrefToUse.addParameter(PaginationConstants.PARAM_SINDEX, column);
        
        String sidx = request.getParameter(PaginationConstants.PARAM_SINDEX);
        String sord = request.getParameter(PaginationConstants.PARAM_SORDER);
        if (column.equals(sidx) && "asc".equalsIgnoreCase(sord)) {
            hrefToUse.addParameter(PaginationConstants.PARAM_SORDER, "desc");
        } else if (column.equals(sidx) && "desc".equalsIgnoreCase(sord)) {
            hrefToUse.addParameter(PaginationConstants.PARAM_SORDER, "asc");
        } else {
            hrefToUse.addParameter(PaginationConstants.PARAM_SORDER, "asc");
        }
        String baseUrl = hrefToUse.toString();
        
        StringBuilder sb = new StringBuilder();
        if (column.equals(sidx) && "asc".equalsIgnoreCase(sord)) {
            // ▲
            sb.append("▲");
        } else if (column.equals(sidx) && "desc".equalsIgnoreCase(sord)) {
            // ▼
            sb.append("▼");
        }
        sb.append("<a class='sortable' href='");
        sb.append(baseUrl);
        sb.append("'>");
        
        return sb.toString();
    }

    public void setColumn(String column) {
        this.column = column;
    }
}
